from strategy_mode import dataLoader, str_cal, Trading, TradingRules

# 下跌模式
def DownMode(df, preTYPES, mode, tradePersent, day_lists, day, statsdic, ran):
    # if TradingRules.MA_Sorter(df, day_lists, day, method="sort", mode="DOWN") <= -3:

    if (
        TradingRules.Sum_inc(df, day) == ("L" or "E")
        and (
            TradingRules.MA_Boll_by(df, day, numL=[3, 5], boll=-1)
            or TradingRules.MA_Key_key_Boll(df, day, numL=[3, 5], boll=-1)
        )
        and statsdic["Share"] > 0
    ):

        mode = "SELL"
        tradePersent = 1

    elif (
        TradingRules.MA_Key_key_Boll(
            df,
            day,
            numL=[TradingRules.MA_LastNoneZero(df, day_lists, day)[0]],
            boll=-1,
        )
        and TradingRules.Main_compare_Both(df, day, -ran, num=4, mode="UP")
        and TradingRules.MA_Key_key_Boll(df, day, numL=[3, 5], boll=1)
    ):
        mode = "BUY"

        if (
            TradingRules.Sum_Rolling(df, day) == "G"
            or TradingRules.Sum_cross(df, day) == "G"
        ):
            tradePersent = 1
        else:
            tradePersent = 0.2
    return df, mode, tradePersent


# 上涨模式
def UpMode(df, preTYPES, mode, tradePersent, day_lists, day, statsdic, ran):
    # elif TradingRules.MA_Sorter(df, day_lists, day, method="sort", mode="UP") >= 3:
    statsdic["TYPES"] = "HIGH"
    # 超级信号
    if (
        statsdic["Share"] > 0
        and (
            TradingRules.Main_compare_or(df, day, ran, num=1.6, mode="UP")
            or TradingRules.Main_Plus(df, day, ran, num=0.3)
        )
        and (
            TradingRules.MA_Key_key_Boll(
                df,
                day,
                numL=[TradingRules.MA_LastNoneZero(df, day_lists, day)[0]],
                boll=1,
            )
        )
    ):

        mode = "SELL"
        tradePersent = 1

    # 变盘
    elif preTYPES == "ROUND" and (
        TradingRules.MA_Key_key_Boll(
            df,
            day,
            numL=[TradingRules.MA_LastNoneZero(df, day_lists, day)[0]],
            boll=-1,
        )
        and TradingRules.Main_compare_Both(df, day, ran, num=2, mode="UP")
    ):
        mode = "BUY"
        if TradingRules.MA_Key_key_Boll(df, day, numL=[3, 5], boll=1):
            tradePersent = 1

    # 普通信号
    elif (
        statsdic["Share"] > 0
        and (
            df.iloc[day].open
            <= df.iloc[day].close_ma_3
            #    and df.iloc[day].open >= df.iloc[day].close
        )
        and TradingRules.MA_Boll_by(df, day, numL=[13, 21], boll=1)
    ):
        mode = "SELL"
        tradePersent = 1
        if TradingRules.MA_Sorter(df, day_lists, day, method="sort", mode="UP") >= 6:
            tradePersent = 0.3
        # elif Ma_Compare(df, day_lists, day, key='close'):
        #     tradePersent = 0.7

    # 追涨
    elif (
        (TradingRules.MA_Key_key_Boll(df, day, numL=[13], boll=1))
        and TradingRules.MA_Boll_by(df, day, numL=[13, 21])
        and TradingRules.Sum_inc(df, day) == ("G" or "E")
        and TradingRules.Main_compare_Both(df, day, ran, num=1.5, mode="DOWN")
    ):
        mode = "BUY"
        # tradePersent = 0.2
        if TradingRules.Sum_cross(df, day) == "G" and TradingRules.MA_Key_key_Boll(
            df, day, numL=[3], boll=-1
        ):
            tradePersent = 0.5
    return df, mode, tradePersent


def RoundMode(df, preTYPES, mode, tradePersent, day_lists, day, statsdic, ran):
    # else:  # 横盘模式
    if TradingRules.MA_Boll_by(df, day, numL=[21, 55], boll=1):  # 上升趋势
        if statsdic["Share"] > 0:
            mode = "SELL"
            if TradingRules.Main_Plus(
                df, day, ran, num=2.5
            ) or TradingRules.Main_compare_or(
                df, day, ran, num=2, mode="UP"
            ):  # 超级信号
                tradePersent = 1
            elif TradingRules.MA_Key_key_Boll(df, day, numL=[21, 55], boll=1):
                tradePersent = 0.8
            elif TradingRules.MA_Key_key_Boll(df, day, numL=[3, 5], boll=1):
                tradePersent = 0.5
                if TradingRules.MA_Key_key_Boll(
                    df, day, numL=[3], boll=1
                ) and TradingRules.MA_Key_key_Boll(
                    df, day, key="low", numL=[3], boll=1
                ):
                    tradePersent = 1

    else:  # 下降趋势

        if (
            statsdic["Share"] > 0
            and TradingRules.Sum_Rolling(df, day) == "X"
            and TradingRules.MA_Boll_by(df, day, numL=[3, 5], boll=-1)
            and TradingRules.MA_Boll_by(df, day, numL=[13, 21], boll=-1)
            and (
                df.iloc[day].open
                <= df.iloc[day].close_ma_3
                #  and df.iloc[day].open >= df.iloc[day].close
            )
        ):
            mode = "SELL"
            if TradingRules.MA_Key_key_Boll(df, day, numL=[3, 5], boll=-1):
                tradePersent = 1
            elif TradingRules.MA_Key_key_Boll(df, day, numL=[3], boll=1):
                tradePersent = 0.5

        elif (
            TradingRules.MA_Boll_by(df, day, numL=[21, 55, 89], boll=1)
            and TradingRules.Sum_inc(df, day) == ("G" or "E")
            # and MA_Key_key_Boll(df, day, numL=[MA_LastNoneZero(df, day_lists, day)[0]],  boll=1)
        ):
            mode = "BUY"
            tradePersent = 0.3
            if (
                TradingRules.Main_Plus(df, day, ran, num=0.5, mode="DOWN")
                and TradingRules.Sum_Rolling(df, day) == "G"
                # and TradingRules.MA_Key_key_Boll(
                #     df, day, numL=[3, 5, 13], boll=1
                # )
            ):
                tradePersent = 1
            elif TradingRules.Sum_Rolling(df, day) == "G" and TradingRules.MA_Boll_by(
                df, day, numL=[3, 5], boll=-1
            ):
                tradePersent = 0.8
            elif TradingRules.Main_compare_or(df, day, ran, num=0.5, mode="DOWN"):
                tradePersent = 0.5

            elif TradingRules.Sum_cross(
                df, day
            ) == "G" and TradingRules.MA_Key_key_Boll(df, day, numL=[3, 5], boll=-1):
                tradePersent = 0.7
    return df, mode, tradePersent
def Main(df, preTYPES, mode, tradePersent, day_lists, day, statsdic, ran):
            # 下跌模式
    if TradingRules.MA_Sorter(df, day_lists, day, method="sort", mode="DOWN") <= -3:
        statsdic["TYPES"] = "LOW"
        df, mode, tradePersent = DownMode(
            df, preTYPES, mode, tradePersent, day_lists, day, statsdic, ran
        )

    # 上涨模式
    elif TradingRules.MA_Sorter(df, day_lists, day, method="sort", mode="UP") >= 3:
        statsdic["TYPES"] = "HIGH"
        df, mode, tradePersent = UpMode(
            df, preTYPES, mode, tradePersent, day_lists, day, statsdic, ran
        )

    else:  # 横盘模式
        statsdic["TYPES"] = "ROUND"
        tradePersent = 0
        df, mode, tradePersent = RoundMode(
            df, preTYPES, mode, tradePersent, day_lists, day, statsdic, ran
        )

    return df, mode, tradePersent